﻿using DevExpress.XtraTab;
using FastReport;
using HiComponents.IEvolution;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.Factory.Presenter;
using WCFMINERP.InnerFactoryContracts.DataContracts;

namespace WCFMINERP.Factory
{
    public partial class frmMain : Form
    {      

        public frmMain()
        {
            InitializeComponent();

            if (Properties.Settings.Default.FactoryId == 0)  //启动激活程序
            {
                FrmActive login = new FrmActive();
                login.ShowDialog();
            }

            InnterManagePresenter.FactoryId = Properties.Settings.Default.FactoryId;

            try
            {               
                if (login())   //弹出登录框
                {

                }
                else
                {
                    // this.Close();
                    Environment.Exit(-1);
                }
            }
            catch (Exception ex)
            {
                WriteAnalyseLog.WriteLogFile(ex.Message);
            }
        }


      

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        private bool login()
        {
            bool b = false;
            frmLogin login = new frmLogin();
            login.ShowDialog();

            if (InnterManagePresenter.UserInfo != null && InnterManagePresenter.UserInfo.UserId>0)
            {
                b = true;
            }
            else
            {
                b = false;
            }
            return b;
        }

    

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Help.ShowImg))
            {
                item_showImg.CheckState = CheckState.Checked;//默认选中
                Help.ShowImg = true.ToString();
            }
            else
            {
                if (Convert.ToBoolean(Help.ShowImg))
                {
                    item_showImg.CheckState = CheckState.Checked;
                }
                else
                {
                    item_showImg.CheckState = CheckState.Unchecked;
                }
            }

            if (string.IsNullOrEmpty(Help.isPring))
            {
                Check_print.CheckState = CheckState.Checked;//默认选中
                Help.isPring = true.ToString();
            }
            else
            {
                if (Convert.ToBoolean(Help.isPring))
                {
                    Check_print.CheckState = CheckState.Checked;
                }
                else
                {
                    Check_print.CheckState = CheckState.Unchecked;
                }
            }

            扫描ToolStripMenuItem_Click(null, null);

        }

        /// <summary>
        /// 扫描
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 扫描ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string text = 扫描ToolStripMenuItem.Text;
            UCProductCommit frm = new UCProductCommit();
            AddUserControl(frm, "UCProductCommit", "扫描条码", text);            
        }

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="Xuser"></param>
        /// <param name="name"></param>
        /// <param name="caption"></param>
        /// <param name="groupName"></param>
        public void AddUserControl(DevExpress.XtraEditors.XtraUserControl Xuser, string name, string caption, string groupName)
        {
            try
            {
                int i = GetTabName(name, groupName);  //查询Tab控件中是否有此 name属性  上面的函数查找
                if (i != -1)
                {
                    xtraTabControl.SelectedTabPageIndex = i;
                    return;
                }
                XtraTabPage page = new XtraTabPage();
                page.Name = name;   //控件标示
                page.Text = caption;  //显示标题
                if (!string.IsNullOrEmpty(groupName))
                {
                    page.Tag = groupName;
                }
                else
                {

                }
                Xuser.Dock = System.Windows.Forms.DockStyle.Fill;  //dock属性 全屏撑大
                page.Controls.Add(Xuser);
                xtraTabControl.TabPages.Add(page);
                xtraTabControl.SelectedTabPage = page;  //首页显示
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// 遍历打开的窗口
        /// </summary>
        /// <param name="value">name值</param>
        /// <returns></returns>
        public int GetTabName(string value, string groupName)
        {
            int count = -1;
            List<XtraTabPage> removeList = new List<XtraTabPage>();
            for (int i = 0; i < xtraTabControl.TabPages.Count; i++)
            {
                if (xtraTabControl.TabPages[i].Tag.ToString() == groupName)
                {
                    xtraTabControl.TabPages[i].Visible = true;
                }
                else
                {
                    removeList.Add(xtraTabControl.TabPages[i]);
                }

            }

            foreach (XtraTabPage xtraTabPage in removeList)
            {
                xtraTabControl.TabPages.Remove(xtraTabPage);
            }

            //xtraTabControl.;
            //xtraTabControl.Refresh();
            for (int i = 0; i < xtraTabControl.TabPages.Count; i++)
            {
                if (xtraTabControl.TabPages[i].Name == value)
                {
                    return i;
                }
            }
            return count;
        }

        /// <summary>
        /// 查询产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             string text = 扫描ToolStripMenuItem.Text;
             UCSercahProduct frm = new UCSercahProduct();
             AddUserControl(frm, "UCSercahProduct", "查询产品", text);            
        }

       
        /// <summary>
        /// 关闭界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xtraTabControl_CloseButtonClick(object sender, EventArgs e)
        {
            xtraTabControl.TabPages.Remove(xtraTabControl.SelectedTabPage);
        }

        /// <summary>
        /// 设置默认打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 默认打印机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSelectPrintName printName = new frmSelectPrintName();
            printName.ShowDialog();
        }

        /// <summary>
        /// 是否显示图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 设置是否显示图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (item_showImg.CheckState == CheckState.Checked)
            {
                Help.ShowImg = true.ToString();
            }

            else if (item_showImg.CheckState == CheckState.Unchecked)
            {
                Help.ShowImg = false.ToString();
            }
            else
            {
                Help.ShowImg = "";
            }
        }

        private void Check_print_Click(object sender, EventArgs e)
        {
            if (Check_print.CheckState == CheckState.Checked)
            {
                Help.isPring = true.ToString();
            }

            else if (item_showImg.CheckState == CheckState.Unchecked)
            {
                Help.isPring = false.ToString();
            }
            else
            {
                Help.isPring = "";
            }
        }
    }
}
